package com.yueya.basic.menu;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.model.Tree;

/**
 * 菜单
 * 
 * @author Administrator
 * 
 */
public class MenuController extends BaseController {

	public void index() {
		render("/WEB-INF/views/basic/menu/menu_list.jsp");
	}

	// 获取菜单Tree
	public void listTree1() {

		List<Tree> list = Menu.service.toTree();
		renderJson(JsonKit.toJson(list));
	}
	
	public void listTree() {
		List<Tree> list = new ArrayList<Tree>();
		list.add(Menu.service.recursiveTree("000"));
		String json = JsonKit.toJson(list);
		renderJson(json);

	}

	// 获取菜单grid
	public void listGrid() {
		String pid = getPara("PID");
		List<Menu> list = null;
		if (null == pid || pid.equals("") || pid.equals("root")) {// 根节点
			list = Menu.service.toGrid();
		} else {
			list = Menu.service.toGridById(pid);
		}
		renderJson(JsonKit.toJson(list));
	}

	// 下拉框选项
	public void treeOfSelect() {
		//List<Tree> list = Menu.service.treeOfSelect();
		List<Tree> list = new ArrayList<Tree>();
		list.add(Menu.service.recursiveTree("000"));
		renderJson(JsonKit.toJson(list));
	}

	// do添加菜单
	public void doAddMenu() {
		String pid = getPara("PID");
		setAttr("PID", pid);
		render("/WEB-INF/views/basic/menu/menu_add.jsp");
	}

	// do编辑菜单
	public void doEditMenu() {
		String id = getPara("id").replace("/", "");
		Menu menu = Menu.dao.findById(id);
		setAttr("menu", menu);
		render("/WEB-INF/views/basic/menu/menu_edit.jsp");
	}

	// 保存菜单
	public void saveMenu() {
		boolean bl = false;
		Menu menu = getModel(Menu.class);
		bl = Menu.service.saveMenu(menu);
		Json json = new Json();
		if (bl) {
			json.setSuccess(true);
			json.setMsg("提交成功！");
		} else {
			json.setSuccess(false);
			json.setMsg("提交失败！");
		}
		renderJson(json);
	}

	// 逻辑删除菜单
	public void removeMenu() {
		boolean bl = false;
		String id = getPara("id");
		if (id != null) {
			bl = Menu.service.removeMenu(id);
		}

		Json json = new Json();
		if (bl) {
			json.setSuccess(true);
			json.setMsg("删除菜单成功！");
		} else {
			json.setSuccess(false);
			json.setMsg("删除菜单失败！");
		}
		renderJson(json);
	}

	// 上移菜单
	@Before(Tx.class)
	public void upMenu() {
		String id = getPara("id");
		Menu menu = Menu.dao.findById(id);
		Menu upmenu = Menu.service.getUpMenu(menu);
		Json json = new Json();
		json.setSuccess(true);
		if (upmenu != null) {
			int order_id = menu.getInt("order_id");
			menu.set("order_id", upmenu.get("order_id"));
			upmenu.set("order_id", order_id);
			menu.update();
			upmenu.update();
			json.setMsg("上移菜单成功！");
		} else {
			json.setMsg("菜单到了顶部无法移动！");
		}
		renderJson(json);

	}

	// 下移菜单
	public void downMenu() {
		String id = getPara("id");
		Menu menu = Menu.dao.findById(id);
		Menu downmenu = Menu.service.getDownMenu(menu);
		Json json = new Json();
		json.setSuccess(true);
		if (downmenu != null) {
			int order_id = menu.getInt("order_id");
			menu.set("order_id", downmenu.get("order_id"));
			downmenu.set("order_id", order_id);
			menu.update();
			downmenu.update();
			json.setMsg("下移菜单成功！");
		} else {
			json.setMsg("菜单到底部无法移动！");
		}
		renderJson(json);
	}

}
